//
//  UICommendDataSource.m
//  tcpoctest
//
//  Created by gump on 5/21/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "UICommendDataSource.h"

#import "SearchFirendVC.h"
#import "personalInfoVC.h"
#import "firendViewController.h"
#import "xxViewController.h"
#import "configViewController.h"
#import "ChatViewController.h"
#import "LoginViewController.h"
#import "MainViewController.h"
#import "setViewController.h"
#import "CustomStatusBar.h"
#import "WriteStatusViewController.h"
#import "CommentViewController.h"
#import "OnlinePeopleView.h"
#import "selectGiftView.h"
#import "ListInfoGiftView.h"
#import "giftSendView.h"
#import "RankingListView.h"
#import "StatusListViewController.h"
#import "PersonalStatusViewController.h"
#import "CommentsListViewController.h"
#import "serverViewController.h"
#import "telepathyView.h"
#import "LoveView.h"

#import "VersionDataSource.h"
#import "taskDataSource.h"

@implementation UICommendDataSource

-(id)init
{
    self = [super init];
    if (self != nil) {
        UICommandArr = [[NSMutableArray alloc] init];
        pthread_mutex_init(&mutexUC, 0);
    }
    
    return self;
}

-(void)addUICommand:(int)command
{
    pthread_mutex_lock(&mutexUC);
    
    [UICommandArr addObject:[NSNumber numberWithInt:command]];
    
    pthread_mutex_unlock(&mutexUC);
}

-(void)addUICommandTryLock:(int)command
{
    if(pthread_mutex_trylock(&mutexUC) == 0)
    {
        
        [UICommandArr addObject:[NSNumber numberWithInt:command]];
        
        pthread_mutex_unlock(&mutexUC);
    }
    
}

-(void)processCommand
{
    pthread_mutex_lock(&mutexUC);
    
    if([UICommandArr count] > 0)
    {
        NSNumber *commandnum = [UICommandArr objectAtIndex:0];
        [self processUICommand:[commandnum intValue]];
        
        [UICommandArr removeObjectAtIndex:0];
    }
    
    pthread_mutex_unlock(&mutexUC);
}

-(void)processUICommand:(int)icommend
{
    switch (icommend)
    {
        case UC_CONFIGFAIL:
        {
            [[ViewManager sharedViewManager] ConfigFail];
        }
            break;
        case UC_SHOWMAIN:
        {
            [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
            WINDOW_HIGTH = [[UIScreen mainScreen] bounds].size.height;
            
            [[ViewManager sharedViewManager] ClearShowViewArr];
            
            [[ViewManager sharedViewManager] showMain:true];
        }
            break;
        case UI_LOGINFAIL:
        {
            [[ViewManager sharedViewManager] LoginFail:[DataControl shareControl].iLoginFailID];
        }
            break;
        case UI_UPDATEFIRENDV:
        {
            [[ViewManager sharedViewManager] reloadFirendView];
        }
            break;
        case UI_SEARCHFIREND:
        {
            [[ViewManager sharedViewManager].searchFirendVC processSearchFirend];
        }
            break;
        case UI_SEARCHFIRENDHEAD:
        {
            [[ViewManager sharedViewManager].personalvc UpdateHeadImage];
        }
            break;
        case UI_SEARCHFAIL:
        {
            [[ViewManager sharedViewManager] searchFirendFail];
        }
            break;
        case UI_ADDFIRENDSUCCESS:
        {
            [[ViewManager sharedViewManager].personalvc addFriendSucess];
            [[ViewManager sharedViewManager].firendvc refreshView];
        }
            break;
        case UC_DELETEFIREND:
        {
            [[ViewManager sharedViewManager].personalvc deleteFirendSucess];
            [[ViewManager sharedViewManager].firendvc refreshView];
        }
            break;
        case UC_RECIVECHECK:
        {
            [[ViewManager sharedViewManager] addXMDabge];
            [[ViewManager sharedViewManager].xxvc refreshView];
        }
            break;
        case UC_REGISTUSEREXIST:
        {
            [[ViewManager sharedViewManager].configvc registUserExist];
        }
            break;
        case UC_REGISTUSERSUCCESS:
        {
            [[ViewManager sharedViewManager].configvc registUserSuccess];
        }
            break;
        case UC_UPDATASYSTEMLIST:
        {
            [[ViewManager sharedViewManager].xxvc.mtablev reloadData];
        }
            break;
        case UC_UPDATEPI:
        {
            [[ViewManager sharedViewManager] PICVsetLabelText];
        }
            break;
        case UC_SETSUCCESS:
        {
            [[ViewManager sharedViewManager] ISSetSuccess:true];
        }
            break;
        case UC_SETVIEW_SETFAIL:
        {
            [[ViewManager sharedViewManager] ISSetSuccess:false];
        }
            break;
        case UC_UPDATEBAFINFO:
        {
            [[ViewManager sharedViewManager].xxvc.mtablev reloadData];
        }
            break;
        case UC_UPDATACHAT:
        {
            [[ViewManager sharedViewManager].chatvc reloadChatView];
        }
            break;
        case UC_LOGINOTHER:
        {
            [[ViewManager sharedViewManager] showView:VI_LOGIN
                                     withIsRemoveCurV:true
                                        withDirection:VD_RIGHT];
            
            [[ViewManager sharedViewManager] showFailHUDWithStatus:@"你的账号在别处登陆"
                                                         withDelay:2
                                                          withView:[ViewManager sharedViewManager].loginvc.view];
        }
            break;
        case UC_UPDATENEARPEOPLE:
        {
            [[ViewManager sharedViewManager] reloadNearPeople];
        }
            break;
        case UC_LOCALLOGIN:
        {
            [[ViewManager sharedViewManager] LocallyLogin:false];
        }
            break;
        case UC_REFRESHCURVIEW:
        {
            if ([ViewManager sharedViewManager].currentVC != nil)
            {
                [[ViewManager sharedViewManager].currentVC refreshView];
            }
        }
            break;
        case UC_SETBADGE:
        {
            [[ViewManager sharedViewManager].mainvc setXMMessageNum:[DataControl shareControl].mBadgeCount];
        }
            break;
        case UC_DELETECACHEOVER:
        {
            [[ViewManager sharedViewManager].setvc deleteCacheOver];
        }
            break;
        case UC_REPORTSUCCESS:
        {
            if ([ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].personalvc)
            {
                [[ViewManager sharedViewManager].personalvc ReportSuccess];
            }
        }
            break;
        case UC_WARNINGNET:
        {
            if ([ViewManager sharedViewManager].currentVC != nil) {
                
                [[ViewManager sharedViewManager].currentVC WarnningNetBreak];
            }
        }
            break;
        case UC_WARNINGSERVER:
        {
            if ([ViewManager sharedViewManager].currentVC != nil) {
                [[ViewManager sharedViewManager].currentVC WarnningServerBreak];
            }
        }
            break;
        case UC_READBIGSUCCESS:
        {
            if ([ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].personalvc) {
                [[ViewManager sharedViewManager].personalvc ReadBigImageSucess];
            }
        }
            break;
        case UC_PROMPT_LOGIN:
        {
            [[ViewManager sharedViewManager].custompromptbar showStatusMessage:@"登陆成功了耶" withDelay:2];
        }
            break;
        case UC_FIRCIR_SENDNEWFAIL:
        {
            if ([ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].writeStatusvc)
            {
                [[ViewManager sharedViewManager].writeStatusvc NewsSendFail];
            }
        }
            break;
        case UC_FIRCIR_SENDNEWSUCCESS:
        {
            if ([ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].writeStatusvc)
            {
                [[ViewManager sharedViewManager].writeStatusvc NewsSendSuccess];
            }
        }
            break;
        case UC_FIRCIR_UPDATESENDHUD:
        {
            if ([ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].writeStatusvc)
            {
                [[ViewManager sharedViewManager].writeStatusvc UpdateHUD];
            }
        }
            break;
        case UC_FIRCIR_COMMENTSUCCESS:
        {
            if ([ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].commentvc) {
                [[ViewManager sharedViewManager].commentvc NewsSendSuccess];
            }
        }
            break;
        case UC_FIRCIR_COMMENTFAIL:
        {
            if ([ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].commentvc) {
                [[ViewManager sharedViewManager].commentvc NewsSendFail];
            }
        }
            break;
        case UC_ALERTVERSION:
        {
            if (![[DataControl shareControl].verDS IsShow]) {
                [[DataControl shareControl].verDS SetIsShow:true];
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新"
                                                                message:[[DataControl shareControl].verDS Content]
                                                               delegate:self
                                                      cancelButtonTitle:@"下次再说"
                                                      otherButtonTitles:@"去看看",nil];
                [alert show];
                
                [[DataControl shareControl].verDS SetIsShow:false];
            }
            
        }
            break;
        case UC_ONLINEPEOBACK:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].onlinepeoplevc) {
                [[ViewManager sharedViewManager].onlinepeoplevc OnlineRequestBakc];
            }
        }
            break;
        case UC_ONLINEP_INITSWITCH:
        {
            [[ViewManager sharedViewManager].onlinepeoplevc InitSwitch];
        }
            break;
        case UC_GIFT_GETLIST:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC == [ViewManager sharedViewManager].giftListInfoVC) {
                [[ViewManager sharedViewManager].giftListInfoVC SelectGiftRequestBakc];
            }
        }
            break;
        case UC_GIFT_REFRESHLIST:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].giftselectvc) {
                [[ViewManager sharedViewManager].giftselectvc refreshView];
            }
        }
            break;
        case UC_GIFT_REFRESHINFO:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].giftListInfoVC) {
                [[ViewManager sharedViewManager].giftListInfoVC refreshView];
            }
        }
            break;
        case UC_GIFT_SENDBACK:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].giftsendvc) {
                [[ViewManager sharedViewManager].giftsendvc SendBack];
            }
        }
            break;
        case UC_CHAT_POPGIFTMENU:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].chatvc) {
                [[ViewManager sharedViewManager].chatvc PopGiftSheet];
            }
        }
            break;
        case UC_CHAT_POPRESENDMENU:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].chatvc) {
                [[ViewManager sharedViewManager].chatvc PopResendSheet];
            }
        }
            break;
        case UC_RANKLIST_RECEIVE:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].rankListVC) {
                [[ViewManager sharedViewManager].rankListVC ReceiveRanklistRequest];
            }
        }
            break;
        case UC_FIRCIR_PYQ_BEGINDEL:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].friendStatusListvc) {
                [[ViewManager sharedViewManager].friendStatusListvc BeginDelNew];
            }
        }
            break;
        case UC_FIRCIR_PYQ_ENDDEL:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].friendStatusListvc) {
                [[ViewManager sharedViewManager].friendStatusListvc EndDelNew];
            }
        }
            break;
        case UC_FIRCIR_PER_BEGINDEL:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].personalStatusvc) {
                [[ViewManager sharedViewManager].personalStatusvc BeginDelNew];
            }
        }
            break;
        case UC_FIRCIR_PER_ENDDEL:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].personalStatusvc) {
                [[ViewManager sharedViewManager].personalStatusvc EndDelNew];
            }
        }
            break;
        case UC_TASK_COMPLETE:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"恭喜"
                                                            message:
                                  [DataControl shareControl].taskDS.strTaskText
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
        }
            break;
        case UC_CHAT_REFRESH:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].chatvc) {
                [[ViewManager sharedViewManager].chatvc refreshView];
            }
        }
            break;

        case UC_ONLINEP_CAMPELELOADING:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].onlinepeoplevc) {
                [[ViewManager sharedViewManager].onlinepeoplevc FirstLoadingData];
            }
        }
            break;

        case UC_FIRCIR_STATE_PACKUP:
        {
            if ([ViewManager sharedViewManager].currentVC != nil)
            {
                if([ViewManager sharedViewManager].currentVC ==
                   [ViewManager sharedViewManager].friendStatusListvc) {
                    [[ViewManager sharedViewManager].friendStatusListvc PackUpCell];
                }
                else if([ViewManager sharedViewManager].currentVC ==
                        [ViewManager sharedViewManager].personalStatusvc)
                {
                    [[ViewManager sharedViewManager].personalStatusvc PackUpCell];
                }
                else if([ViewManager sharedViewManager].currentVC ==
                        [ViewManager sharedViewManager].commentsListvc)
                {
                    [[ViewManager sharedViewManager].commentsListvc PackUpCell];
                }
            }
        }
            break;
            case UC_FIRCIR_BADGE:
        {
            if ([ViewManager sharedViewManager].currentVC != nil) {
                if ([ViewManager sharedViewManager].currentVC !=
                    [ViewManager sharedViewManager].friendStatusListvc) {
                    
                    [[ViewManager sharedViewManager].mainvc ShowFirCirBadge];
                    [[ViewManager sharedViewManager].servervc refreshView];
                    
                }
            }
        }
            break;
            case UC_TELEP_GETCANDLE:
        {
            if ([ViewManager sharedViewManager].currentVC != nil) {
                if ([ViewManager sharedViewManager].currentVC ==
                    [ViewManager sharedViewManager].loveVC) {
                    
                    [[ViewManager sharedViewManager].loveVC CandleBack];
                    
                }
            }
        }
            break;
            case UC_TELEP_GETLIGHT:
        {
            if ([ViewManager sharedViewManager].currentVC != nil) {
                if ([ViewManager sharedViewManager].currentVC ==
                    [ViewManager sharedViewManager].loveVC) {
                    
                    [[ViewManager sharedViewManager].loveVC LightBack];
                    
                }
            }
        }
            break;
            case UC_CHAT_TELEPATHYFAIL:
        {
            if ([ViewManager sharedViewManager].currentVC != nil &&
                [ViewManager sharedViewManager].currentVC ==
                [ViewManager sharedViewManager].chatvc) {
                [[ViewManager sharedViewManager].chatvc TelepthyFail];
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark -alertview delegate-
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *url = [[DataControl shareControl].verDS Url];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    }
    
    [[DataControl shareControl].verDS SetIsShow:false];
}

@end


